﻿using System;
using System.IO;
using System.Reflection;

namespace sosql
{
    class Parameters
    {
        const string trustedConnectionString = "Data Source={0},{1};Initial Catalog={2};Integrated Security=True";
        const string sqlConnectionString = "Data Source={0},{1};Initial Catalog={2};User Id={3};Password={4}";
        const string fileNotFound = "File \"{0}\" not found.";

        string server = "(local)";
        string username = string.Empty;
        string password = string.Empty;
        string database = string.Empty;
        string inputData = string.Empty;
        int port = 1433;
        bool hasInput = false;
        bool trustedConnection = false;
        bool suppressNumbering = false;
        bool setQuotedIdentifier = false;
        bool verbose = false;
        bool echoInput = false;

        public Parameters(string[] param)
        {
            StreamReader sr;
            for(int paramLoop=0; paramLoop<param.Length; paramLoop++)
            {
                string p = param[paramLoop].Substring(0, 2);
                string val = string.Empty;
                // handles spaces in params
                switch (p)
                {
                    case "/E":
                    case "/e":
                    case "/n":
                    case "/I":
                    case "/v":
                    case "/?":
                    case "-E": 
                    case "-e" :
                    case "-n" :
                    case "-I" :
                    case "-v" :
                    case "-?" :
                        break; // no additional param, skip
                    default:
                        if(paramLoop+1<param.Length)
                            val = param[paramLoop + 1];
                        if (param[paramLoop].Length > 2)
                            val = param[paramLoop].Substring(2);
                        else
                            paramLoop++;
                        break;
                }

                switch (p)
                {
                    case "/n":
                    case "-n":
                        suppressNumbering = true;
                        break;
                    case "/I":
                    case "-I":
                        setQuotedIdentifier = true;
                        break;
                    case "/S" :
                    case "-S":
                        server = val;
                        break;
                    case "/d":
                    case "-d":
                        database = val;
                        break;
                    case "/Q":
                    case "-Q":
                        hasInput = true;
                        inputData = val;
                        inputData += Environment.NewLine + "GO" + Environment.NewLine; // force a GO on the end
                        break;
                    case "/i":
                    case "-i":
                        if (File.Exists(val))
                        {
                            sr = new StreamReader(val);
                            inputData = sr.ReadToEnd();
                            sr.Close();
                            hasInput = true;
                        }
                        else
                            Console.WriteLine(String.Format(fileNotFound, val));
                        break;
                    case "/e" :
                    case "-e":
                        echoInput = true;
                        break;
                    case "/E" :
                    case "-E":
                        trustedConnection = true;
                        break;
                    case "/U" :
                    case "-U":
                        username = val;
                        break;
                    case "/P":
                    case "-P":
                        password = val;
                        break;
                    case "/v" :
                    case "-v":
                        verbose = true;
                        break;
                    case "/?" :
                    case "-?":
                        Assembly myAssembly = Assembly.GetExecutingAssembly();
                        sr = new StreamReader(myAssembly.GetManifestResourceStream("sosql.UsageText.txt"));
                        Console.WriteLine(String.Format(sr.ReadToEnd(), myAssembly.GetName().Version.ToString()));
                        sr.Close();
                        Environment.Exit(0);
                        break;
                }
            }
        }

        public string ConnectionString
        {
            get
            {
                string retVal = string.Empty;
                if (trustedConnection)
                    retVal = String.Format(trustedConnectionString, server, port, database);
                else
                    retVal = String.Format(sqlConnectionString, server, port, database, username, password);
                return retVal;
            }
        }

        public bool HasInput
        {
            get
            {
                return hasInput;
            }
        }

        public string InputData
        {
            get
            {
                return inputData;
            }
        }

        public bool SuppressNumbering
        {
            get
            {
                return suppressNumbering;
            }
        }

        public bool SetQuotedIdentifier
        {
            get
            {
                return setQuotedIdentifier;
            }
        }

        public bool EchoInput
        {
            get
            {
                return echoInput;
            }
        }

        public bool Verbose
        {
            get
            {
                return verbose;
            }
        }
    }
}
